package common.project.epic.ui;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import de.android1.overlaymanager.ManagedOverlay;
import de.android1.overlaymanager.ManagedOverlayGestureDetector;
import de.android1.overlaymanager.ManagedOverlayItem;
import de.android1.overlaymanager.OverlayManager;
import de.android1.overlaymanager.ZoomEvent;


public class EpicMapActivity extends MapActivity {
	private MapView mapView;
	private MapController mapController;
	
	private double inLat;
	private double inLon;
	private double outLat;
	private double outLon;
	
	private boolean settableMsgPosition = false;
	
	private static final double signetLat = 45.4088;
	private static final double signetLon = 11.8945;
	private GeoPoint signetGeoPoint;
	
	private GeoPoint inGeoPoint;
	private GeoPoint outGeoPoint;
	
	private TextView mapLocation_TV;
	private Button mapDoneButton;
	private Button mapCancelButton;
	
//	EpicSimpleOverlay eso;
	OverlayManager overlayManager;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent  callingIntent = getIntent();
		inLat = callingIntent.getDoubleExtra("inLat", signetLat);
		inLon = callingIntent.getDoubleExtra("inLon", signetLon);
		settableMsgPosition = callingIntent.getBooleanExtra("settableMsgPosition",false);
		
		if(settableMsgPosition) {
			setContentView(R.layout.map_layout);
			mapLocation_TV = (TextView)findViewById(R.id.mapLocation_TV);
			mapDoneButton = (Button)findViewById(R.id.mapDoneButton);
			mapDoneButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(settableMsgPosition) {
						Intent result = new Intent();
						outLat = outGeoPoint.getLatitudeE6()/(1E6);
						outLon = outGeoPoint.getLongitudeE6()/(1E6);
						result.putExtra("outLat", outLat);
						result.putExtra("outLon", outLon);
						setResult(RESULT_OK, result);
					}
					finish();
				}
			});
			mapCancelButton = (Button)findViewById(R.id.mapCancelButton);
			mapCancelButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(settableMsgPosition) {
//						Intent result = new Intent();
//						outLat = inGeoPoint.getLatitudeE6()/(1E6);
//						outLon = inGeoPoint.getLongitudeE6()/(1E6);
//						result.putExtra("outLat", outLat);
//						result.putExtra("outLon", outLon);
						setResult(RESULT_CANCELED, null);
					}
					finish();
				}
			});
		}
		else {
			setContentView(R.layout.epicshowmsg_map_layout);
		}
		
		inGeoPoint = new GeoPoint((int)(inLat*1E6), (int)(inLon*1E6));
		outGeoPoint = inGeoPoint;
		
		int sLat = 	(int)(signetLat*1E6);
		int sLon = (int)(signetLon*1E6);
		signetGeoPoint = new GeoPoint(sLat, sLon);
		
//		Criteria criteria = new Criteria();
//		criteria.setAccuracy(Criteria.ACCURACY_COARSE);
//		criteria.setPowerRequirement(Criteria.POWER_LOW);
//		criteria.setAltitudeRequired(false);
//		criteria.setBearingRequired(false);
//		criteria.setSpeedRequired(false);
//		criteria.setCostAllowed(true);		
//		String context = Context.LOCATION_SERVICE;
//		locationManager = (LocationManager)getSystemService(context);
//		provider =  locationManager.getBestProvider(criteria, true);
//		location = locationManager.getLastKnownLocation(provider);
		
		mapView = (MapView)findViewById(R.id.map_view);
		mapView.setSatellite(true);
		mapView.displayZoomControls(true);
		
		mapController = mapView.getController();
//		mapController.setZoom(18);
		mapController.setCenter(inGeoPoint);
		
		overlayManager = new OverlayManager(this, mapView);
	}
	
	private ManagedOverlay managedOverlay;
	private ManagedOverlayItem msgMarker;
	@Override
	public void onStart() {
		super.onStart();
	    Drawable defaultmarker = getResources().getDrawable(R.drawable.geo_pointer_48x48);     
	    managedOverlay = overlayManager.createOverlay(defaultmarker);
	    
	    managedOverlay.setOnOverlayGestureListener(new ManagedOverlayGestureDetector.OnOverlayGestureListener(){

			@Override
			public boolean onDoubleTap(MotionEvent event, ManagedOverlay mo, GeoPoint geoPoint, ManagedOverlayItem moItem) {
				if(settableMsgPosition) {
					managedOverlay.remove(msgMarker);
					outGeoPoint = geoPoint;
					managedOverlay.createItem(geoPoint);	    
				    //registers the ManagedOverlayer to the MapView
				    overlayManager.populate();			
				    double latitude = outGeoPoint.getLatitudeE6()/(1E6);
					double longitude = outGeoPoint.getLongitudeE6()/(1E6);
					String msgOrigin = "Latitude:" + latitude + "\nLongitude:" + longitude;
				    mapLocation_TV.setText("Message origin will be:\n" + msgOrigin);
				}
			    return false;
			}

			@Override
			public void onLongPress(MotionEvent arg0, ManagedOverlay arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onLongPressFinished(MotionEvent arg0, ManagedOverlay arg1, GeoPoint arg2, ManagedOverlayItem arg3) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public boolean onScrolled(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3, ManagedOverlay arg4) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public boolean onSingleTap(MotionEvent arg0, ManagedOverlay arg1, GeoPoint arg2, ManagedOverlayItem arg3) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public boolean onZoom(ZoomEvent arg0, ManagedOverlay arg1) {
				// TODO Auto-generated method stub
				return false;
			}
	    });
	    
	    managedOverlay.createItem(inGeoPoint);	    
	    mapController.animateTo(inGeoPoint);
	    //registers the ManagedOverlayer to the MapView
	    overlayManager.populate();
	    double latitude = inGeoPoint.getLatitudeE6()/(1E6);
		double longitude = inGeoPoint.getLongitudeE6()/(1E6);
		String msgOrigin = "Latitude:" + latitude + "\nLongitude:" + longitude;
		if(settableMsgPosition) {
			mapLocation_TV.setText("Message origin will be:\n" + msgOrigin);
		}
	}
	
	@Override
	protected boolean isRouteDisplayed() {
	// IMPORTANT: This method must return true if your Activity
	// is displaying driving directions. Otherwise return false.
	return false;
	}
}
